package cn.vconinfo.base.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuxg@qq.com on 2017/6/21.
 */
public class CollectionUtils {

    private CollectionUtils() {
    }

    public static boolean isEmpty(List source) {
        if (null == source || source.size() == 0) {
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(List source) {
        return !isEmpty(source);
    }

    public static List<Long> getSampleIdListBeyondTwoIdList(List<Long> idList1, List<Long> idList2) {

        if (org.springframework.util.CollectionUtils.isEmpty(idList1) || org.springframework.util.CollectionUtils.isEmpty(idList2)) {
            return new ArrayList<>();
        }

        List<Long> sampleIdList = new ArrayList<>();

        if (idList1.size() > idList2.size()) {
            for (Long item : idList2) {
                if (idList1.contains(item)) {
                    sampleIdList.add(item);
                }
            }
        } else {
            for (Long item : idList1) {
                if (idList2.contains(item)) {
                    sampleIdList.add(item);
                }
            }
        }

        return sampleIdList;
    }

    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remaider = source.size() % n;
        int number = source.size() / n;
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remaider > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remaider--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

}
